
package com.manpower.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

@ApiModel(value = "服务器响应数据")
public class RestResponse implements Serializable {
    private static final long serialVersionUID = -5816304413090422336L;
    @ApiModelProperty(value = "执行状态：0-成功 1-异常")
    private int code;
    @ApiModelProperty(value = "响应的消息")
    private String msg;
    @ApiModelProperty(value = "附加数据")
    private Object data;
    public static final RestResponse SUCCESS = new RestResponse("");

    public RestResponse() {
    }

    public RestResponse(Object data) {
        this.code = 0;
        this.msg = "success";
        if (data instanceof BusinessException) {
            BusinessException be = (BusinessException)data;
            this.code = be.getErrorCode();
            this.msg = be.getMsg();
        } else if (data instanceof Exception) {
            Exception e = (Exception)data;
            this.code = 500;
            this.msg = e.getMessage();
        } else {
            this.data = data;
        }

    }

    public RestResponse(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public RestResponse(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Object getData() {
        return this.data;
    }

    public void setData(Object data) {
        if (this != SUCCESS) {
            this.data = data;
        }

    }

    public int getCode() {
        return this.code;
    }

    public void setCode(int code) {
        if (this != SUCCESS) {
            this.code = code;
        }

    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        if (this != SUCCESS) {
            this.msg = msg;
        }

    }

    public String toString() {
        return "RestResponse [code=" + this.code + ", msg=" + this.msg + ", data=" + this.data + "]";
    }
}
